package com.tulele.vtool.task.async;

import com.alibaba.fastjson.JSONObject;
import com.tulele.vtool.bus.model.UserInfo;
import com.tulele.vtool.bus.model.thirdparty.WxNotifyMsgSetInfo;
import com.tulele.vtool.bus.model.thirdparty.WxPushUserInfo;
import com.tulele.vtool.bus.service.IArticleService;
import com.tulele.vtool.bus.service.IUserService;
import com.tulele.vtool.bus.service.UserGroupGzhService;
import com.tulele.vtool.model.UserGroupOpenId;
import com.tulele.vtool.util.RedisCacheUtil;
import com.tulele.vtool.util.SysConstant;
import com.tulele.vtool.util.ToolUtil;
import com.tulele.vtool.wechat.service.WeixinService;
import com.tulele.vtool.wechat.service.WeixinServiceDL;
import com.tulele.vtool.wechat.service.WeixinServiceMap;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Optional;
//11:20 文章推送
@Component
@Slf4j
public class PositiveEnergyArticlePushAsyncTask {

    @Autowired
    private RedisCacheUtil redisCacheUtil;

    @Autowired
    private IArticleService articleService;

    @Autowired
    private ObjectFactory<WeixinServiceDL> objectFactory;

    @Autowired
    private IUserService userService;

    @Autowired
    private UserGroupGzhService userGroupGzhService;

    //20200401 hyl 改成同步发 因为多线程 数据库顶不住
    //20200512 调整参数，多线程跑
    @Async("taskExecutor")
    public void execute(String redisKey, UserInfo userInfo, String profile) throws Exception {

        WxPushUserInfo wxPushUserInfo  = new WxPushUserInfo();
        wxPushUserInfo.setOpenId(userInfo.getOpenId());

        UserGroupOpenId ugoId = userGroupGzhService.getUserOpenId(profile, userInfo.getUserId());
        if (ugoId != null) {
            wxPushUserInfo.setOpenId(ugoId.getOpenId());
        }

        wxPushUserInfo.setUserId(userInfo.getUserId());

        if (wxPushUserInfo != null) {

            String kefuMsg = articleService.positiveEnergyMsgString(wxPushUserInfo.getUserId(), profile);
            // String kefuMsg = articleService.generateUserPushArticleInfoV3(wxPushUserInfo.getUserId(), 5);
            if (!kefuMsg.equals("")) {
                if(userInfo.getPhoneModel().equals(SysConstant.IOS)){
                    kefuMsg = ToolUtil.handleMsgmenuidByIOS(kefuMsg);
                }
                WxMpKefuMessage message = WxMpKefuMessage.TEXT().toUser(wxPushUserInfo.getOpenId()).content(kefuMsg).build();
                try {
                    WeixinServiceDL weixinService = WeixinServiceMap.getWeixinService(profile);
                    if (weixinService == null) {
                        weixinService = objectFactory.getObject();
                        weixinService.reSetConfigStorage(profile);
                        WeixinServiceMap.putWeixinService(profile, weixinService);
                    }
                    weixinService.getKefuService().sendKefuMessage(message);
                } catch (WxErrorException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //20200401 hyl 改成同步发 因为多线程 数据库顶不住
    //20200512 调整参数，多线程跑
    @Async("taskExecutor")
    public void executeUserArticleType(String redisKey, UserInfo userInfo, String profile, Integer articleTypeId) throws Exception {

        WxPushUserInfo wxPushUserInfo  = new WxPushUserInfo();
        wxPushUserInfo.setOpenId(userInfo.getOpenId());

        UserGroupOpenId ugoId = userGroupGzhService.getUserOpenId(profile, userInfo.getUserId());
        if (ugoId != null) {
            wxPushUserInfo.setOpenId(ugoId.getOpenId());
        }

        wxPushUserInfo.setUserId(userInfo.getUserId());

        if (wxPushUserInfo != null) {

            String kefuMsg = articleService.getUserArticleTypeMsgString(wxPushUserInfo.getUserId(), profile, articleTypeId);
            // String kefuMsg = articleService.generateUserPushArticleInfoV3(wxPushUserInfo.getUserId(), 5);
            if (!kefuMsg.equals("")) {
                if(userInfo.getPhoneModel().equals(SysConstant.IOS)){
                    kefuMsg = ToolUtil.handleMsgmenuidByIOS(kefuMsg);
                }
                WxMpKefuMessage message = WxMpKefuMessage.TEXT().toUser(wxPushUserInfo.getOpenId()).content(kefuMsg).build();
                try {
                    WeixinServiceDL weixinService = WeixinServiceMap.getWeixinService(profile);
                    if (weixinService == null) {
                        weixinService = objectFactory.getObject();
                        weixinService.reSetConfigStorage(profile);
                        WeixinServiceMap.putWeixinService(profile, weixinService);
                    }
                    weixinService.getKefuService().sendKefuMessage(message);
                } catch (WxErrorException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //发开单学堂的文章
    @Async("taskExecutor")
    public void execute_KDXT(String redisKey,UserInfo userInfo, String profile) throws Exception {

        WxPushUserInfo wxPushUserInfo  = new WxPushUserInfo();
        wxPushUserInfo.setOpenId(userInfo.getOpenId());

        UserGroupOpenId ugoId = userGroupGzhService.getUserOpenId(profile, userInfo.getUserId());
        if (ugoId != null) {
            wxPushUserInfo.setOpenId(ugoId.getOpenId());
        }

        wxPushUserInfo.setUserId(userInfo.getUserId());

        if (wxPushUserInfo != null) {
            String kefuMsg = articleService.generateUserPushArticleInfo_KDXT(wxPushUserInfo.getUserId(), profile);
            // String kefuMsg = articleService.generateUserPushArticleInfoV3(wxPushUserInfo.getUserId(), 5);
            if (!kefuMsg.equals("")) {
                if(userInfo.getPhoneModel().equals(SysConstant.IOS)){
                    kefuMsg = ToolUtil.handleMsgmenuidByIOS(kefuMsg);
                }
                WxMpKefuMessage message = WxMpKefuMessage.TEXT().toUser(wxPushUserInfo.getOpenId()).content(kefuMsg).build();
                try {
                    WeixinServiceDL weixinService = WeixinServiceMap.getWeixinService(profile);
                    if (weixinService == null) {
                        weixinService = objectFactory.getObject();
                        weixinService.reSetConfigStorage(profile);
                        WeixinServiceMap.putWeixinService(profile, weixinService);
                    }
                    weixinService.getKefuService().sendKefuMessage(message);
                } catch (WxErrorException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Async("taskExecutor")
    public void execute_brand(String redisKey,UserInfo userInfo, String profile, String brandId) throws Exception {

        WxPushUserInfo wxPushUserInfo  = new WxPushUserInfo();
        wxPushUserInfo.setOpenId(userInfo.getOpenId());

        UserGroupOpenId ugoId = userGroupGzhService.getUserOpenId(profile, userInfo.getUserId());
        if (ugoId != null) {
            wxPushUserInfo.setOpenId(ugoId.getOpenId());
        }

        wxPushUserInfo.setUserId(userInfo.getUserId());

        if (wxPushUserInfo != null) {
            String kefuMsg = articleService.generateUserPushArticleInfo_Brand(wxPushUserInfo.getUserId(), profile, brandId);
            if (!kefuMsg.equals("")) {
                if(userInfo.getPhoneModel().equals(SysConstant.IOS)){
                    kefuMsg = ToolUtil.handleMsgmenuidByIOS(kefuMsg);
                }
                WxMpKefuMessage message = WxMpKefuMessage.TEXT().toUser(wxPushUserInfo.getOpenId()).content(kefuMsg).build();
                try {
                    WeixinServiceDL weixinService = WeixinServiceMap.getWeixinService(profile);
                    if (weixinService == null) {
                        weixinService = objectFactory.getObject();
                        weixinService.reSetConfigStorage(profile);
                        WeixinServiceMap.putWeixinService(profile, weixinService);
                    }
                    weixinService.getKefuService().sendKefuMessage(message);
                } catch (WxErrorException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
